import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testserviceaccount:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/1_get_serviceaccount_list.yaml'))
    def test_get_serviceaccount_list(self, serviceaccountinfo):
        # 获取serviceaccount列表接口-正常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/2_create_serviceaccount.yaml'))
    def test_create_serviceaccount(self, serviceaccountinfo):
        # 创建serviceaccount接口-正常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceaccountinfo['request']['method']
        data = serviceaccountinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        serviceaccountname = req.json()['metadata']['name']
        globals()['serviceaccountname'] = serviceaccountname
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/3_get_specific_serviceaccount.yaml'))
    def test_get_specific_serviceaccount(self, serviceaccountinfo):
        # 获取指定serviceaccount接口-正常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        updatereqbody = req.json()
        globals()['updatereqbody'] = updatereqbody
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/4_update_serviceaccount.yaml'))
    def test_update_serviceaccount(self, serviceaccountinfo):
        # 更新指定serviceaccount接口-正常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        serviceaccountinfo['request']['data'] = globals()['updatereqbody']
        data = serviceaccountinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/5_update_section_serviceaccount.yaml'))
    def test_update_section_serviceaccount(self, serviceaccountinfo):
        # 部分更新指定serviceaccount的接口-正常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        data = serviceaccountinfo['request']['data']
        header = serviceaccountinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/6_delete_specific_serviceaccount.yaml'))
    def test_delete_specific_serviceaccount(self, serviceaccountinfo):
        # 删除serviceaccount接口-正常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/1_get_serviceaccount_list_exception.yaml'))
    def test_get_serviceaccount_list_exception(self, serviceaccountinfo):
        # 获取serviceaccount列表接口-异常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/2_create_serviceaccount_exception.yaml'))
    def test_create_serviceaccount_exception(self, serviceaccountinfo):
        # 创建serviceaccount接口-异常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default')
        method = serviceaccountinfo['request']['method']
        data = serviceaccountinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/3_get_specific_serviceaccount_exception.yaml'))
    def test_get_specific_serviceaccount_exception(self, serviceaccountinfo):
        # 获取指定serviceaccount接口-异常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/4_update_serviceaccount_exception.yaml'))
    def test_update_serviceaccount_exception(self, serviceaccountinfo):
        # 更新指定serviceaccount接口-异常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        serviceaccountinfo['request']['data'] = globals()['updatereqbody']
        data = serviceaccountinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/5_update_section_serviceaccount_exception.yaml'))
    def test_update_section_serviceaccount_exception(self, serviceaccountinfo):
        # 部分更新指定serviceaccount的接口-异常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        data = serviceaccountinfo['request']['data']
        header = serviceaccountinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.serviceaccount
    @pytest.mark.parametrize('serviceaccountinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/serviceaccount/6_delete_specific_serviceaccount_exception.yaml'))
    def test_delete_specific_serviceaccount_exception(self, serviceaccountinfo):
        # 删除serviceaccount接口-异常
        base_url = datas.baseUrl + Testserviceaccount.url_unify
        url = base_url + serviceaccountinfo['request']['url'].replace('{namespace}', 'default').replace(
            '{serviceaccountname}', globals()['serviceaccountname'])
        method = serviceaccountinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == serviceaccountinfo['validate'][0]['equals']['status_code']
